/****************************************************************************
 * 2025.4 阿桑的笔记本
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;
using Unity.VisualScripting;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

namespace QFramework.Example
{
	public partial class GunPanel : UIElement,IController,ICanInsIcon,ICanBtnOnclickEvents
	{
        /// <summary>
        /// 资源加载
        /// </summary>
        ResLoader resLoader;
		private void Awake()
		{
            CoinText.text = "点数：" + DataManager.Instance.gameDatas.Coin;
            resLoader = ResLoader.Allocate();
            var sp = resLoader.LoadSync<SpriteAtlas>("WapenIcons");
            InsIcon(sp);
            BtnOnClicks();

        }

		protected override void OnBeforeDestroy()
		{
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }

        public void InsIcon(SpriteAtlas spriteAtlas)
        {

           // Debug.Log(spriteAtlas.)

			foreach (var item in DataManager.Instance.wapenSystems.wapenItems)
			{
                GunBtns.InstantiateWithParent(GunRoot).Self(self =>
                {

                    var selfChage = self;
                    var itemChage = item;

                    ///设置对应的图标

                    //为按钮添加悬停事件
                    selfChage.AddComponent<EventTrigger>();
                    ///获取悬停事件
                    var trigger = selfChage.GetComponent<EventTrigger>();
                    //设定事件类型为鼠标进入
                    EventTrigger.Entry entry = new EventTrigger.Entry();

                    entry.eventID = EventTriggerType.PointerEnter;

                    //entry.callback.AddListener((data) => { OnPointerEnterDelegate((WapenUpGrade)data); } );
                    entry.callback.AddListener((data) => OnPointerEnterDelegate(itemChage,spriteAtlas));
                    trigger.triggers.Add(entry);




                    self.GetComponent<Image>().sprite = 
                    spriteAtlas.GetSprite(item.WapenIconName);
                    self.GetComponent<Image>().type = Image.Type.Simple;
                    self.GetComponent<Image>().preserveAspect = true;
                    self.GetComponent<Image>().SetNativeSize();
                    //itemChage.isUnLocked = DataManager.Instance.gameDatas.is
                    itemChage.isUnLocked = DataManager.Instance.gameDatas.GetUnLockeds(itemChage.WapenName);
                    if (itemChage.isUnLocked)
                    {
                        selfChage.GetComponentInChildren<Text>().gameObject.SetActive(false);
                    }
                    else
                    {
                        selfChage.GetComponentInChildren<Text>().gameObject.SetActive(true);
                    }
                    selfChage.onClick.AddListener(() =>
                    {
                        ///先判断是否解锁了
                        if (itemChage.isUnLocked)
                        {
                            ///跳转场景
                            Debug.Log("点击了"+itemChage.WapenName);
                            ///发布解锁武器命令
                          
                            this.Hide();
                            UIKit.ClosePanel<UI_StartPanel>();
                            UIKit.ClosePanel<UI_StartUIPanel>();
                            DataManager.Instance.gameDatas.GunName = itemChage.WapenName;
                            Debug.Log("选择了：" + DataManager.Instance.gameDatas.GunName);
                            SceneManager.LoadScene(1);
                            ///关闭画布
                            //this.SendCommand(new WapenSetCommands(itemChage.WapenName));
                            

                        }
                        else
                        {
                            ///1.钱不够，无法解锁
                            ///2.钱够，解锁，存档，
                            if (DataManager.Instance.gameDatas.Coin<itemChage.Price)
                            {
                                Debug.Log("金币不够");
                            }
                            else
                            {
                                Debug.Log( "剩余金钱"+(DataManager.Instance.gameDatas.Coin -= itemChage.Price));
                                CoinText.text = "点数：" + DataManager.Instance.gameDatas.Coin;
                                itemChage.isUnLocked = true;
                                DataManager.Instance.gameDatas.SetUnLocked(itemChage.WapenName);
                                DataManager.Instance.saveSystem.SaveData(DataManager.Instance.
                                    gameDatas, "gameData.json");
                                selfChage.GetComponentInChildren<Text>().gameObject.SetActive(false);

                            }
                            
                        }
                        

                    });


                }).Show();

            }
        }

        private void OnPointerEnterDelegate(WapenItem wanepen, SpriteAtlas spriteAtlas)
        {
            GunDataText.text = wanepen.WapenData;
            GunNameText.text = wanepen.WapenName;

        }

        public void BtnOnClicks()
        {
            QuitBtn.onClick.AddListener(() =>
            {
                this.Hide();
            });
        }
    }
}